package edu.pdx.LAMA.AndroidCourse.scheduler;

import java.util.Calendar;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;

public class AppCalendarViewFragment extends Fragment {
	
	//private TextView text; 
	private static final String TAG = "AppCalendarViewFragment";
	public static final String EXTRA_SELECTED_DATE = "edu.pdx.LAMA.AndroidCourse.scheduler.calendar.extraSelectedDate";
	private CalendarView calendar; 
	String selectedDate;
	String saverDate;
	
	
	
	OnDateClickListenerFromCalendar dateClickListenerCallBack;
	
	public interface OnDateClickListenerFromCalendar{
		public void onDateClickListenerCallBackFromCalendar(String date);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
	}
	
	public void initializeCalendar(View v){
		//Date date;
		//SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
		long milliTime=0;
		try{
			Log.d(TAG, "************************************");
			Log.d(TAG, "selected date while initializing calendar "+selectedDate);
			//date = formatter.parse(selectedDate);
			String parts[] = selectedDate.split("/");
			int day = Integer.parseInt(parts[0]);
			int month = Integer.parseInt(parts[1]);
			int year = Integer.parseInt(parts[2]);
			//Log.d(TAG, "Day to set: "+day);
			//Log.d(TAG, "Month to set: "+month);
			//Log.d(TAG, "Year to set: "+year);
			Calendar calendar1 = Calendar.getInstance();
			calendar1.set(Calendar.YEAR, year);
			calendar1.set(Calendar.MONTH, month);
			calendar1.set(Calendar.DAY_OF_MONTH, day);
			milliTime = calendar1.getTimeInMillis();
		} catch (ParseException e){
			//date = formatter.parse();
			Log.d(TAG, "Faced a parsing exception");
		} catch (Exception e){
			//date = formatter.parse();
			Log.d(TAG, "Faced some other exception");
		}
		calendar = (CalendarView) v.findViewById(R.id.calendar);
		calendar.setShowWeekNumber(true);
		calendar.setDate(milliTime, false, false);
		calendar.setFirstDayOfWeek(2);
		calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));
		calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
		calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
		calendar.setSelectedDateVerticalBar(R.color.darkgreen);

		calendar.setOnDateChangeListener(new OnDateChangeListener() {
			
			@Override
			public void onSelectedDayChange(CalendarView view, int year, int month,
					int dayOfMonth) {
				// TODO Auto-generated method stub
				saverDate = dayOfMonth + "/" + month + "/" + year;
				Toast.makeText(getActivity(), saverDate, Toast.LENGTH_SHORT).show();
				dateClickListenerCallBack.onDateClickListenerCallBackFromCalendar(saverDate);
			}
		});
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//return super.onCreateView(inflater, container, savedInstanceState);
		selectedDate = getArguments().getString(AppCalendarViewFragment.EXTRA_SELECTED_DATE);
		//Log.d(TAG, "selected date in on create view "+selectedDate);
		View v = inflater.inflate(R.layout.fragment_appcalendar, container, false);
		initializeCalendar(v);
		Log.d(TAG, "inside calendar view fragment");
		return v;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		try{
			dateClickListenerCallBack = (OnDateClickListenerFromCalendar) activity;
		} catch (ClassCastException e){
			Log.d(TAG, "Class Cast Exception");
		} catch(Exception e){
			Log.d(TAG, "Some other exception has occured");
		}
	}
}
